import { reqPapers } from '../api'
const state = {
  // 部分文章的数据
  papers: [],
  // 文章的总数
  total: 0,
  // 所有文章的数据
  allPapers: [],
  // 单篇文章展示的页面
  showAllPapers: []
}
const mutations = {
  // 获取部分文章的数据 
  GETPAPERS(state, params) {
    state.papers = params[0];
    state.total = params[1];
    state.allPapers = params[2];
  },
  // 获取全部文章的数据
  GETALLPAPERS(state, showAllPapers) {
    state.showAllPapers = showAllPapers;
  }
}
const actions = {
  // 获取部分文章的数据
  async getPapers({ commit }, pageNo) {
    let result = await reqPapers();
    // 每次根据传递过来的页数pageNo返回该页数的数据
    var temp = result.data.data.slice(5 * (pageNo - 1), 5 * (pageNo - 1) + 5)
    let total = result.data.data.length;
    let allPapers = result.data.data;
    if (result.code == 200) {
      commit("GETPAPERS", [temp, total, allPapers])
    }
  },
  // 获取全部文章的数据
  async getAllPapers({ commit }) {
    let result = await reqPapers();
    let allPapers = result.data.data;
    if (result.code == 200) {
      commit('GETALLPAPERS', allPapers)
    }
  }
}
const getters = {}
export default {
  state, mutations, actions, getters
}